package com.zone.vehicle.superApps.view.home.recommend;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.zone.sdk.module.appstore.entity.AppEntity;
import com.zone.vehicle.superApps.databinding.FragmentRecommdBinding;
import com.zone.vehicle.superApps.model.HomeViewModel;
import com.zone.vehicle.superApps.model.ViewModelFactory;
import com.zone.vehicle.superApps.view.common.HandledFragment;
import com.zone.vehicle.superApps.view.home.recommend.adapter.RecommendAppAdapter;

import java.util.ArrayList;
import java.util.List;

import static com.zone.vehicle.superApps.view.home.HomeFragment.APP_DETAIL_FRAGMENT;

/**
 * 安利墙
 */

public class RecommendFragment extends HandledFragment {

    private static final String TAG = RecommendFragment.class.getSimpleName();

    private TextView textView;

    private FragmentRecommdBinding binding;

    private RecommendAppAdapter appAdapter;

    private List<AppEntity> appList = new ArrayList<>();

    private HomeViewModel viewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel = new ViewModelProvider(requireActivity(), new ViewModelFactory()).get(HomeViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        Log.d(TAG, "RecommendFragment onCreateView");
        binding = FragmentRecommdBinding.inflate(inflater);

        appAdapter = new RecommendAppAdapter(appList, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppEntity app = (AppEntity)v.getTag();
                viewModel.setCurrentApp(app);
                changePage(APP_DETAIL_FRAGMENT);
            }
        });
        binding.recommendList.setAdapter(appAdapter);
        binding.recommendList.setLayoutManager(new GridLayoutManager(getContext(), 2));
//        binding.recommendList.setLayoutManager(new LinearLayoutManager(getContext()));

        binding.btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "RecommendFragment btnBack onClick");
                onBack();
            }
        });
        viewModel.getRecommendAppList().observe(getViewLifecycleOwner(), new Observer<List<AppEntity>>() {
            @Override
            public void onChanged(List<AppEntity> appEntities) {
                appAdapter.setAppList(appEntities);
                appAdapter.notifyDataSetChanged();
            }
        });
        return binding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        viewModel.commentInitAmwayWallList();
    }

}
